package com.xmall.order.handler.msg;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.xmall.config.AmqpCommonConfig;
import com.xmall.msg.queue.OrderModule;
import com.xmall.msg.entity.order.OrderRefundMsg;
import com.xmall.order.service.other.IOrderTradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 订单退款
 */
@Slf4j
@Component
@RabbitListener(queues = OrderModule.OrderRefund.ORDER_REFUND_QUEUE, containerFactory = AmqpCommonConfig.RABBIT_LISTENER_CONTAINER_FACTORY_NAME)
public class OrderRefundHandler {

    @Resource
    private IOrderTradeService orderTradeService;

    @RabbitHandler
    public void msg(Channel channel, Message message, OrderRefundMsg msg) throws IOException {
        log.info("rabbitmq handler: order service: OrderRefundHandler: {}", JSONUtil.toJsonStr(msg));
        try {
            orderTradeService.executeRefund(msg.getRefundLogId());
        }catch (Exception e) {}
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
